import React, {useEffect} from 'react';
import {StyleSheet, Text, Image, View, Button} from 'react-native';
import SafeAreaView from 'react-native-safe-area-view';
import {useNavigation} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import { load } from '../../utils/Storage';
import UserStore from '../../stores/UserStore';

import icon_main_logo from '../../assets/icon_main_logo.png';
export default () => {
    useEffect(() => {
      setTimeout(() => {
        getUserInfo();
      }, 3000);
    });
   const getUserInfo = async () => {
      // 加载缓存中的用户信息
      const cacheUserInfo = await load('userInfo');
      // 如果没有缓存中的用户信息，则执行登录操作
      if (!cacheUserInfo) {
          startLogin();
      } else {
          // 将缓存中的用户信息解析为JSON对象
          const parse = JSON.parse(cacheUserInfo);
          // 如果解析成功，则将解析后的用户信息存储到UserStore中，并执行主页操作
          if (parse) {
              UserStore.setUserInfo(parse);
              startHome();
          } else {
              // 如果解析失败，则重新执行登录操作
              startLogin();
          }
      }
  }
  const navigation = useNavigation<StackNavigationProp<any>>(); //声明相关泛型
  const startLogin = () => {
    navigation.replace('Login'); //从登录页到跳转替换更好点
  };
  const startHome = () => {
    navigation.replace('MainTab'); //从登录页到跳转替换更好点
  };
  return (
    <SafeAreaView>
      <View style={styles.root}>
        <Image style={styles.logo_main} source={icon_main_logo}></Image>
      </View>
    </SafeAreaView>
  );
};
const styles = StyleSheet.create({
  root: {
    width: '100%',
    height: '100%',
    backgroundColor: 'white',
    flexDirection: 'column',
    alignItems: 'center',
  },
  logo_main: {
    width: 200,
    height: 105,
    marginTop: 200,
    resizeMode: 'contain',
  },
});
